package io.xxx.xbutler.core.callback;

import com.alibaba.fastjson2.JSONObject;
import io.xxx.xbutler.domain.Callback;
import io.xxx.xbutler.domain.Contact;
import io.xxx.xbutler.domain.GenderType;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * 机器人群内主动添加好友回调
 *
 * <pre>
 * {
 *     "vcMerchantNo": "202211150061049",
 *     "vcRobotWxId": "wxid_8yixw5vxbdms22",
 *     "vcRobotSerialNo": "13982D80B3B235000C03C3C0AC6D12BF",
 *     "vcSerialNo": "20231220235945164014327078537",
 *     "nType": 3002,
 *     "nResult": 1,
 *     "vcResult": "加好友成功",
 *     "nTimeStamp": 1703087991598,
 *     "Data": {
 *         "vcAddUserWxId": "",
 *         "vcAddUserSerialNo": "",
 *         "vcHelloWord": "",
 *         "vcNickName": "再见时风起°",
 *         "vcHeadImgUrl": "http://wx.qlogo.cn/mmhead/ver_1/767bMpNFtjAOBtlNQznibHzzjdPEJkIIy708sWA20CiaTjicVTefDqtTp2p8vOGTJVt46AhZmeloII7rnT6uPgHcKDsqInjX5YiaCxhibJeElInXCR90UDV4K7QmpoXJ97gf3/132",
 *         "nSex": 0,
 *         "vcCountry": "CN",
 *         "vcProvince": "Heilongjiang",
 *         "vcCity": "Jixi",
 *         "vcPersonalized": "",
 *         "nType": 10,
 *         "vcAccount": "nz3nF2Wi1G",
 *         "RelationSerialNo": ""
 *     }
 * }
 * </pre>
 */
@Slf4j
@Component
public class _3002Handler extends RobotContactHandler implements CallbackHandler {

    @Override
    public String name() {
        return "机器人群内主动添加好友回调";
    }

    @Override
    public boolean supports(int type) {
        return type == 3002;
    }

    @Override
    public void handle(Callback callback) {
        LocalDateTime now = LocalDateTime.now();
        saveRobotContact(callback, (JSONObject) callback.getData(), now);
    }

    @Override
    protected Contact buildContact(JSONObject data, LocalDateTime now) {
        return new Contact()
                .setId(data.getString("vcAddUserWxId"))
                .setSerialNo(data.getString("vcAddUserSerialNo"))
                .setGender(GenderType.valueOf(data.getInteger("nSex")))
                .setName(data.getString("vcNickName"))
                .setBase64Name(data.getString("vcBase64NickName"))
                .setAvatar(data.getString("vcHeadImgUrl"))
                .setProvince(data.getString("vcProvince"))
                .setCity(data.getString("vcCity"))
                .setCounty(data.getString("vcCounty"))
                .setUpdatedTime(now);
    }
}
